package com.twu.biblioteca;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: abishek1
 * Date: 8/8/13
 * Time: 6:57 PM
 * To change this template use File | Settings | File Templates.
 */
public class Library{
    Book books[] = new Book[10];
    Movie movies[] = new Movie[15];
    User users[] = new User[10];
    User currentUser;
    int numberOfBooks;
    int numberOfMovies;
    int numberOfUsers;
    Library(){
        currentUser = null;
    }

    void addBooks(String bookFileName){
        int i = 0;
        File file = new File(bookFileName);
        try {
            Scanner bookScanner = new Scanner(file);
            while(bookScanner.hasNextLine()){
                String line = bookScanner.nextLine();
                String parts[] = line.split("~");
                int id = Integer.parseInt(parts[0]);
                String bookName = new String(parts[1]);
                String status = new String(parts[2]);
                boolean available;

                books[i] = new Book(id, bookName);
                if(status.equalsIgnoreCase("available"))
                    i++;
                else
                    books[i++].unreserve();
            }
            numberOfBooks = i;
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

    void addMovies(String movieFileName){
        int i = 0;
        File file2 = new File(movieFileName);
        try {
            Scanner movieScanner = new Scanner(file2);
            while(movieScanner.hasNextLine()){
                String line = movieScanner.nextLine();
                String parts[] = line.split("~");
                String movieName = new String(parts[0]);
                int year = Integer.parseInt(parts[1]);
                String director = new String(parts[2]);
                int rating = Integer.parseInt(parts[3]);
                movies[i++] = new Movie(movieName, year, director, rating);
            }
            numberOfMovies = i;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    User getCurrentUser(){
        return currentUser;
    }

    void displayAllBooks(){
        System.out.println(numberOfBooks);
        for(int i = 0; i < numberOfBooks; i++){
            books[i].displayBook();
        }
    }

    void displayAllMovies(){
        System.out.print("Name\t\tYear\t\tDirector\t\tRating\n");
        for(int i = 0; i < numberOfMovies; i++){
            movies[i].displayMovie();
        }
    }

    void displayAvailableBooks(){
        System.out.println("Available Books");
        for(int i = 0; i < numberOfBooks; i++){
            if(books[i].isAvailable())
                books[i].displayBook();
        }
    }

    boolean reserveBook(int id) {
        for(int i = 0; i < numberOfBooks; i++){
            if(books[i].getId() == id && books[i].isAvailable()){
                books[i].unreserve();
                return true;
            }
        }
        return false;
    }

    void writeToFile(){
        File file = new File("Books.txt");
        FileWriter fw = null;
        try {
            fw = new FileWriter(file.getAbsoluteFile());

            BufferedWriter bw = new BufferedWriter(fw);

            for(int i = 0; i < numberOfBooks; i++){
            books[i].writeBookToFile(bw);
        }
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    void initializeLoginInfo(String userFileName){
        int i = 0;
        File file = new File(userFileName);
        try {
            Scanner userScanner = new Scanner(file);
            while(userScanner.hasNextLine()){
                String line = userScanner.nextLine();
                String parts[] = line.split("~");
                String username = new String(parts[0]);
                String password = new String(parts[1]);
                String emailId = new String(parts[2]);
                String phoneNo = new String(parts[3]);
                users[i++] = new User(username, password, emailId, phoneNo);
            }
            numberOfUsers = i;
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

    public boolean performLogin(String username, String password) {
        for(int i = 0; i < numberOfUsers; i++){
            if(users[i].getName().equals(username) && users[i].getPassword().equals(password)){
                currentUser = users[i];
                return true;
            }
        }
        return false;
    }

    public void login() {
        initializeLoginInfo("UserList.txt");
        System.out.println("Username:");
        Scanner inputScanner = new Scanner(System.in);
        String username = inputScanner.nextLine();
        System.out.println("Password:");
        String password = inputScanner.nextLine();
        boolean pass = performLogin(username, password);
        if(pass == true){
            System.out.println("Successfully Logged in as " + username);
        }
        else
            System.out.println("Login failed");
    }

    public void logout() {
        currentUser = null;
    }


}
